<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/boot.js"></script>
</head>

<body>
    <div class="layui-fluid">
        <div class="flexrow">
            <div class="layui-panel list-container">
                <div class="list-search-container">
                    <button class="layui-btn layui-btn-sm" onclick="HandleAdd()"><i class="layui-icon layui-icon-add-1 layui-font-12"></i>新增定时任务</button>
                    <div class="list-search-item">
                        <span>任务名称：</span>
                        <input id="job_name" autocomplete="off" class="layui-input">
                    </div>
                    <button class="layui-btn layui-btn-sm" style="margin-left:5px" onclick="BindTableData()"><i class="layui-icon layui-icon-search layui-font-12"></i>搜索</button>
                </div>
                <table class="layui-table" lay-data="{id: 'datagrid'}" lay-filter="datagrid">
                    <thead>
                        <tr>
                            <th lay-data="{field:'job_name'}">任务名称</th>
                            <th lay-data="{fixed: 'right', width:120, align:'center', toolbar: '#job_type'}">任务类别</th>
                            <th lay-data="{field:'execute_peroid',width:120,align:'center'}">执行间隔</th>
                            <th lay-data="{fixed: 'right', width:100, align:'center', toolbar: '#execute_unit'}">间隔单位</th>
                            <th lay-data="{field:'corn_express', width:150}">表达式</th>
                            <th lay-data="{fixed: 'right', width:120, align:'center', toolbar: '#job_status'}">任务状态</th>
                            <th lay-data="{field:'sort_num',width:80,align:'center'}">排序</th>
                            <th lay-data="{fixed: 'right', width:275, align:'center', toolbar: '#op'}">操作</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</body>
<script type="text/html" id="job_type">
{{#
        return "<div>"+GetEnumCodeText('定时任务类别',d.job_type) +"</div>" 
    }}
</script>
<script type="text/html" id="execute_unit">
{{#
        return "<div>"+GetEnumCodeText('定时任务间隔单位',d.execute_unit) +"</div>" 
    }}
</script>
<script type="text/html" id="job_status">
    <div lay-event="HandleChangeStatus">
        {{# if(d.job_status==10){ }}
            <input type="checkbox" name="job_status" lay-skin="switch" lay-text="运行|停止" value={{ d.job_status }} lay-filter="job_status" checked>
            {{# } else { }}
                <input type="checkbox" name="job_status" lay-skin="switch" lay-text="运行|停止" value={{ d.job_status}} lay-filter="job_status">
                {{# } }}
    </div>
</script>
<script type="text/html" id="op">
    <button class="layui-btn layui-btn-sm" lay-event="HandleExecute"><i class="layui-icon layui-icon-edit layui-font-12"></i>执行一次</button>
    <button class="layui-btn layui-btn-sm" lay-event="HandleEdit"><i class="layui-icon layui-icon-edit layui-font-12"></i>编辑</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="HandleDelete"><i class="layui-icon layui-icon-delete layui-font-12"></i>删除</button>
</script>
<script>
    $(function() {
        BindTableData();
    })

    function BindTableData() {
        var param = {
            job_name: $("#job_name").val(),
        }
        BindTable("datagrid", "system/common/job/list", param);
    }


    function HandleAdd() {
        OpenDialog('新增定时任务', 'system/job/job_add.html', 800, 600, function() {
            BindTableData();
        })
    }

    function HandleEdit(RowData) {
        OpenDialog('修改任务', 'system/job/job_edit.html?row_id=' + RowData.row_id, 800, 600, function() {
            BindTableData();
        })
    }

    function HandleDelete(RowData) {
        if (RowData.job_status == 10) {
            return OpenWarningMessage("任务正在执行，请先关闭");
        }
        OpenConfirm('确认删除？', function() {
            var param = {
                row_id: RowData.row_id
            }
            HttpPost(JSON.stringify(param), 'system/common/job/delete', function(result) {
                if (result.code == 0) {
                    OpenSuccessMessage("删除成功")
                    BindTableData();
                } else {
                    OpenFail(result.msg);
                }
            })
        })
    }

    function HandleChangeStatus(RowData) {
        var param = {
            row_id: RowData.row_id,
            job_status: RowData.job_status == 10 ? 20 : 10
        }
        HttpPost(JSON.stringify(param), 'system/common/job/updateStatus', function(result) {
            if (result.code == 0) {
                OpenSuccessMessage("修改成功")
                BindTableData();
            } else {
                OpenFail(result.msg);
            }
        })
    }

    function HandleExecute(RowData){
        var param = {
            row_id: RowData.row_id,
        }
        HttpPost(JSON.stringify(param), 'system/common/job/executeOnce', function(result) {
            if (result.code == 0) {
                OpenSuccessMessage("执行成功")
            } else {
                OpenFail(result.msg);
            }
        })
    }
</script>

</html>